package com.xuan.webwork.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xuan.webwork.entitys.Teacher;
import com.xuan.webwork.repository.TeacherRepository;
import com.xuan.webwork.service.TeacherService;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    private final TeacherRepository teacherRepository;

    @Autowired
    public TeacherServiceImpl(TeacherRepository teacherRepository) {
        this.teacherRepository = teacherRepository;
    }

    @Override
    public List<Teacher> getAllTeachers() {
        return teacherRepository.findAll();
    }

    @Override
    public Optional<Teacher> getTeacherById(Long id) {
        return teacherRepository.findById(id);
    }

    @Override
    public Optional<Teacher> getTeacherByTeacherId(String teacherId) {
        return teacherRepository.findByTeacherId(teacherId);
    }

    @Override
    public Teacher saveTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    @Override
    public void deleteTeacherById(Long id) {
        teacherRepository.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteTeacherByTeacherId(String teacherId) {
        teacherRepository.deleteByTeacherId(teacherId);
    }

    @Override
    public List<Teacher> findTeachersByName(String name) {
        return teacherRepository.findByNameContaining(name);
    }

    @Override
    public List<Teacher> findTeachersByDepartment(String department) {
        return teacherRepository.findByDepartment(department);
    }

    @Override
    public boolean existsByTeacherId(String teacherId) {
        return teacherRepository.existsByTeacherId(teacherId);
    }

    @Override
    public List<Teacher> findTeachersByConditions(String name, String department, String title) {
        return teacherRepository.findTeachersByConditions(name, department, title);
    }
}
